package duppentity.dto.recharge;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import constants.BuDefault;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/9 16:57
 */

public class BuRechargeCouponDtoUtil {
    public static List<BuRechargeCouponDto> getBuRechargeCouponDtoList(JsonObject data) {
        List<BuRechargeCouponDto> buRechargeCouponDtoList = null;
        if (data != null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        buRechargeCouponDtoList = new ArrayList<>();
                        for (Integer i = 0; i < list.size(); i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            BuRechargeCouponDto buRechargeCouponDto = convertBuRechargeCouponDto(listOne, data);
                            buRechargeCouponDtoList.add(buRechargeCouponDto);
                        }
                    }
                }
            }
        }
        return buRechargeCouponDtoList;
    }


    private static BuRechargeCouponDto convertBuRechargeCouponDto(JSONObject listOne, JsonObject data) {
        BuRechargeCouponDto buRechargeCouponDto = new BuRechargeCouponDto();

        buRechargeCouponDto.id = listOne.getLongValue("id");
        buRechargeCouponDto.couponId = listOne.getLongValue("couponId");
        if (buRechargeCouponDto.couponId > 0) {
            JSONObject buCoupon = JSONObjectUtils.get(data, "bu_coupon");
            buRechargeCouponDto.couponName = JSONObjectUtils.getString(buCoupon, buRechargeCouponDto.couponId.toString(), "name");

            Integer status =JSONObjectUtils.getInteger(buCoupon, buRechargeCouponDto.couponId.toString(), "status");
            buRechargeCouponDto.couponStatusName = BuDefault.getResultName(status,BuDefault.nameType.status);

        }
        buRechargeCouponDto.rechargeId = listOne.getLongValue("rechargeId");
        buRechargeCouponDto.orderIndex = listOne.getIntValue("orderIndex");
        buRechargeCouponDto.num = listOne.getIntValue("num");


        return buRechargeCouponDto;
    }
}

